/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.domain.action.user;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.portalcenter.domain.das.*;
import cn.hallele.ms.portalcenter.domain.domain.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/6 13:13
 */
@Slf4j
@Service
public class UserDomainService {
    @Autowired
    private UserDDDas userDDDas;

    @Autowired
    private UserRoleDEDas userRoleDEDas;

    @Autowired
    private RoleDEDas roleDEDas;

    @Autowired
    private RoleMenuDEDas roleMenuDEDas;

    @Autowired
    private MenuDEDas menuDEDas;

    /**
     * 获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    public UserDomain queryUserByUsername(String username) {
        return userDDDas.queryUserByUsername(username);
    }

    /**
     * 获取用户头像
     *
     * @param username 用户名
     * @return 用户信息
     */
    public String getAvatar(String username) {
        return userDDDas.getAvatar(username);
    }

    /**
     * 通过用户名查询完整的用户信息，包括角色、菜单
     *
     * @param username 用户名
     * @return 完整的用户信息
     */
    public UserDomain queryUserFullInfo(String username) {
        UserDomain userDomain = userDDDas.queryUserByUsername(username);

        if (userDomain == null) {
            return null;
        }
        fillRoles(userDomain);
        fillMenus(userDomain);

        return userDomain;
    }

    private void fillRoles(UserDomain userDomain) {
        List<UserRoleDomain> userRoleDomains = userRoleDEDas.findByUserId(userDomain.getId());
        if (ObjectUtil.isEmpty(userRoleDomains)) {
            return;
        }

        List<RoleDomain> roles = roleDEDas.findByIdIn(userRoleDomains.stream()
                .map(UserRoleDomain::getRoleId)
                .collect(Collectors.toSet()));

        if (ObjectUtil.isEmpty(roles)) {
            return;
        }

        userDomain.setRoles(new HashSet<>(roles));
    }

    private void fillMenus(UserDomain userDomain) {
        Set<RoleDomain> roles = userDomain.getRoles();
        if (ObjectUtil.isEmpty(roles)) {
            return;
        }

        List<RoleMenuDomain> roleMenuDomains = roleMenuDEDas.findByRoleIdIn(roles.stream()
                .map(RoleDomain::getId)
                .collect(Collectors.toSet()));
        if (ObjectUtil.isEmpty(roleMenuDomains)) {
            return;
        }

        List<MenuDomain> menus = menuDEDas.findByIdIn(roleMenuDomains.stream()
                .map(RoleMenuDomain::getMenuId)
                .collect(Collectors.toSet()));

        if (ObjectUtil.isEmpty(menus)) {
            return;
        }

        userDomain.setMenus(new HashSet<>(menus));
    }

    /**
     * 用户分页查询
     *
     * @param userQueryBO userQueryBO
     * @return UserDTO
     */
    public Page<UserDomain> listUser(UserQueryDomain userQueryBO) {
        return userDDDas.listUser(userQueryBO);
    }
}
